package com.wing.productionPlan.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.JsonResult;
import com.wing.productionPlan.dao.ProductionCategoryDao;
import com.wing.productionPlan.model.entity.ProductionCategory;
import com.wing.productionPlan.model.request.ProductionCategoryForm;
import com.wing.productionPlan.model.response.ProductionCategoryVO;
import com.wing.qualityTesting.model.entity.ProcessQualityCategory;
import com.wing.qualityTesting.model.response.NodeDataVO;
import com.wing.qualityTesting.model.response.ProcessQualityCategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 质检分类-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class ProductionCategoryServiceImpl extends ServiceImpl<ProductionCategoryDao, ProductionCategory> implements ProductionCategoryService {

    @Override
    public int add(ProductionCategoryForm qualityTestingCategoryForm) {

        //校验

        ProductionCategory qualityTestingCategory = new ProductionCategory();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(ProductionCategory.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(qualityTestingCategoryForm, qualityTestingCategory, copyOptions);

        return baseMapper.insert(qualityTestingCategory);
    }

    @Override
    public List<ProductionCategoryVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<ProductionCategoryVO> pageList4app(Page page, Map<String, Object> params) {
        List<ProductionCategoryVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public ProductionCategoryVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    @Transactional
    public int enable(Long id) {
        ProductionCategoryVO productionCategoryVO = baseMapper.get4appById(id);
        if (productionCategoryVO != null) {
            ProductionCategory productionCategory = new ProductionCategory();
            productionCategory.setId(id);
            productionCategory.setIsDeleted(true);
            int updateCount = baseMapper.updateById(productionCategory);
            ProductionCategory productionCategoryParent = new ProductionCategory();
            productionCategoryParent.setId(productionCategoryVO.getParentId());
            productionCategoryParent.setHasEnableChild(true);
            baseMapper.updateById(productionCategoryParent);
            return updateCount;
        }
        return 0;
    }

    @Override
    @Transactional
    public int disable(Long id) {
        ProductionCategoryVO productionCategoryVO = baseMapper.get4appById(id);
        if (productionCategoryVO != null) {
            ProductionCategory productionCategory = new ProductionCategory();
            productionCategory.setId(id);
            productionCategory.setIsDeleted(false);
            int updateCount = baseMapper.updateById(productionCategory);
            int count = baseMapper.selectCount(new QueryWrapper<ProductionCategory>().eq("parent_id", productionCategory.getParentId()).eq("is_deleted", true));
            if (count == 0) {
                ProductionCategory productionCategoryParent = new ProductionCategory();
                productionCategoryParent.setId(productionCategoryVO.getParentId());
                productionCategoryParent.setHasEnableChild(false);
                baseMapper.updateById(productionCategoryParent);
            }
            return updateCount;
        }
        return 0;
    }

    public JsonResult getTreeData(Long productionCategoryId) {
        List<Long> productionCategoryIdList = new ArrayList<>();
        if (productionCategoryId != null) {
            List<ProductionCategory> roleList = this.baseMapper.getRoleHierarchy(productionCategoryId);
            productionCategoryIdList = roleList.stream().map(t -> t.getId()).collect(Collectors.toList());
        }
        QueryWrapper<ProductionCategory> queryWrapper = new QueryWrapper();
        if (productionCategoryId != null) {
            queryWrapper.notIn("id", productionCategoryIdList);
        }
        List<ProductionCategory> list = this.baseMapper.selectList(queryWrapper);
        Map<Long, List<NodeDataVO>> map = new HashMap<>();
        for (ProductionCategory productionCategory : list) {
            if (map.containsKey(productionCategory.getParentId())) {
                NodeDataVO nodeDataVO = new NodeDataVO();
                nodeDataVO.setValue(productionCategory.getId().toString());
                nodeDataVO.setName(productionCategory.getName());
                map.get(productionCategory.getParentId()).add(nodeDataVO);
            } else {
                List<NodeDataVO> tmpList = new ArrayList<>();
                NodeDataVO nodeDataVO = new NodeDataVO();
                nodeDataVO.setValue(productionCategory.getId().toString());
                nodeDataVO.setName(productionCategory.getName());
                tmpList.add(nodeDataVO);
                map.put(productionCategory.getParentId(), tmpList);
            }
        }
        List<NodeDataVO> rootList = map.get(-1L);
        if (rootList == null) {
            return JsonResult.success();
        }
        constructTree(rootList, map);
        return JsonResult.success(rootList);
    }

    private void constructTree(List<NodeDataVO> parentList, Map<Long, List<NodeDataVO>> dataMap) {
        for (NodeDataVO node : parentList) {
            Long nodeId = Long.valueOf(node.getValue());
            List<NodeDataVO> childList = dataMap.get(nodeId);
            if (childList != null) {
                node.setChildren(childList);
                constructTree(childList, dataMap);
            }
        }
    }

    @Override
    public List<ProductionCategory> getRoleHierarchy(Long productionCategoryId) {
        return this.baseMapper.getRoleHierarchy(productionCategoryId);
    }

}
